package commands;

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import vo.Comment;
import vo.Photo;
import vo.User;
import vo.Vote;

public class DropTablesCommand implements Comando {

	@Override
	public String execute(String[] args) throws IOException {
		
		if (args.length != 1) {
			return getSintaxe();
		}
		
		String saida = "";
		saida  = this.dropTabela(User.TABLENAME);
		saida += this.dropTabela(Photo.TABLENAME);
		saida += this.dropTabela(Comment.TABLENAME);
		saida += this.dropTabela(Vote.TABLENAME);
		
		return saida;		
	}
	
	private String dropTabela(String tabela) throws IOException{
		HBaseAdmin hBaseAdmin = new HBaseAdmin(HBaseConfiguration.create());
		if(hBaseAdmin.tableExists(tabela)){
			hBaseAdmin.disableTable(tabela);
			hBaseAdmin.deleteTable(tabela);
			return tabela+"\n";
		}
		return "Tabela Inexistente"+"\n";
	}

	@Override
	public String getKey() {
		return "-dropTables";
	}

	@Override
	public String getSintaxe() {
		return "-dropTables";
	}

}
